﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CAGFIP.Edicao;
using CAGFIP.Utils;

namespace CAGFIP.GUI
{
    public class CRUD : Panel
    {

        #region Properties

        public DataGridView Perguntas
        {
            get { return this.perguntasView; }
        }

        public DataGridView Assuntos
        {
            get { return this.assuntosView; }
        }

        #endregion

        #region Constructors
        
        public CRUD()
        {
            this.Dock = DockStyle.Fill;
            this.Location = new System.Drawing.Point(0, 0);
            initComponents();
        }

        private void initComponents()
        {
            #region DataGridView Config

            perguntasViewEnunciado = new DataGridViewTextBoxColumn() { Name = "enunciado" };
            perguntasViewAssunto = new DataGridViewComboBoxColumn() { Name = "assunto" };
            perguntasViewAlternativaCorreta = new DataGridViewTextBoxColumn() { Name = "altCorreta" };
            perguntasViewAlternativasIncorretas = new DataGridViewTextBoxColumn[Constants.QtdeAlternativasIncorretas];
            for (int i = 0; i < Constants.QtdeAlternativasIncorretas; i++)
                perguntasViewAlternativasIncorretas[i] = new DataGridViewTextBoxColumn() { Name = "altIncorreta" + i };

            perguntasView = new DataGridView();

            perguntasView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            perguntasView.Columns.Add(this.perguntasViewEnunciado);
            perguntasView.Columns.Add(this.perguntasViewAssunto);
            perguntasView.Columns.Add(this.perguntasViewAlternativaCorreta);
            for (int i = 0; i < Constants.QtdeAlternativasIncorretas; i++)
                perguntasView.Columns.Add(this.perguntasViewAlternativasIncorretas[i]);

            perguntasView.Location = new System.Drawing.Point(7, 20);
            perguntasView.Size = new System.Drawing.Size(411, 291);
            ((System.ComponentModel.ISupportInitialize)(perguntasView)).BeginInit();
            //perguntasView.ReadOnly = true;
            perguntasView.AllowUserToAddRows = perguntasView.AllowUserToDeleteRows = perguntasView.AllowUserToResizeRows = false;
            perguntasView.RowStateChanged += new DataGridViewRowStateChangedEventHandler(perguntasView_RowStateChanged);

            assuntosView = new DataGridView();
            assuntosView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            assuntosView.Location = new System.Drawing.Point(7, 19);
            assuntosView.Size = new System.Drawing.Size(411, 102);
            ((System.ComponentModel.ISupportInitialize)(assuntosView)).BeginInit();
            //assuntosView.ReadOnly = true;

            /*
            this.dataGridView1.AllowUserToOrderColumns = true;
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.Enunciado,
            this.Assunto,
            this.AlternativaCorreta,
            this.AlternativaIncorreta1,
            this.AlternativaIncorreta2,
            this.AlternativaIncorreta3,
            this.AlternativaIncorreta4});
            this.dataGridView1.Location = new System.Drawing.Point(29, 71);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(240, 150);
            this.dataGridView1.TabIndex = 0;
             
             */
            #endregion

            #region Label Config

            enunciadoLabel = new Label();
            enunciadoLabel.AutoSize = true;
            enunciadoLabel.Location = new System.Drawing.Point(481, 22);
            enunciadoLabel.Size = new System.Drawing.Size(58, 13);
            enunciadoLabel.Text = "Enunciado";

            alternativaIncorreta1Label = new Label();
            alternativaIncorreta1Label.AutoSize = true;
            alternativaIncorreta1Label.Location = new System.Drawing.Point(481, 119);
            alternativaIncorreta1Label.Size = new System.Drawing.Size(111, 13);
            alternativaIncorreta1Label.Text = "Alternativa Incorreta 1";

            alternativaIncorreta2Label = new Label();
            alternativaIncorreta2Label.AutoSize = true;
            alternativaIncorreta2Label.Location = new System.Drawing.Point(481, 158);
            alternativaIncorreta2Label.Size = new System.Drawing.Size(111, 13);
            alternativaIncorreta2Label.Text = "Alternativa Incorreta 2";

            alternativaIncorreta3Label = new Label();
            alternativaIncorreta3Label.AutoSize = true;
            alternativaIncorreta3Label.Location = new System.Drawing.Point(481, 197);
            alternativaIncorreta3Label.Size = new System.Drawing.Size(111, 13);
            alternativaIncorreta3Label.Text = "Alternativa Incorreta 3";

            alternativaIncorreta4Label = new Label();
            alternativaIncorreta4Label.AutoSize = true;
            alternativaIncorreta4Label.Location = new System.Drawing.Point(481, 236);
            alternativaIncorreta4Label.Size = new System.Drawing.Size(111, 13);
            alternativaIncorreta4Label.Text = "Alternativa Incorreta 4";

            alternativaCorretaLabel = new Label();
            alternativaCorretaLabel.AutoSize = true;
            alternativaCorretaLabel.Location = new System.Drawing.Point(481, 275);
            alternativaCorretaLabel.Size = new System.Drawing.Size(94, 13);
            alternativaCorretaLabel.Text = "Alternativa Correta";

            assuntoLabel = new Label();
            assuntoLabel.AutoSize = true;
            assuntoLabel.Location = new System.Drawing.Point(477, 50);
            assuntoLabel.Size = new System.Drawing.Size(90, 13);
            assuntoLabel.Text = "Nome do assunto";

            assuntoDescricaoLabel = new Label();
            assuntoDescricaoLabel.AutoSize = true;
            assuntoDescricaoLabel.Location = new System.Drawing.Point(477, 89);
            assuntoDescricaoLabel.Size = new System.Drawing.Size(90, 13);
            assuntoDescricaoLabel.Text = "Descrição do assunto";

            #endregion

            #region TextBox Config

            enunciadoPergunta = new TextBox();
            enunciadoPergunta.Location = new System.Drawing.Point(484, 38);
            enunciadoPergunta.Multiline = true;
            enunciadoPergunta.Size = new System.Drawing.Size(277, 78);
            enunciadoPergunta.TabIndex = 6;

            alternativaIncorreta1 = new TextBox();
            alternativaIncorreta1.Location = new System.Drawing.Point(484, 135);
            alternativaIncorreta1.Size = new System.Drawing.Size(277, 20);
            alternativaIncorreta1.TabIndex = 7;

            alternativaIncorreta2 = new TextBox();
            alternativaIncorreta2.Location = new System.Drawing.Point(484, 174);
            alternativaIncorreta2.Size = new System.Drawing.Size(277, 20);
            alternativaIncorreta2.TabIndex = 8;

            alternativaIncorreta3 = new TextBox();
            alternativaIncorreta3.Location = new System.Drawing.Point(484, 213);
            alternativaIncorreta3.Size = new System.Drawing.Size(277, 20);
            alternativaIncorreta3.TabIndex = 9;

            alternativaIncorreta4 = new TextBox();
            alternativaIncorreta4.Location = new System.Drawing.Point(484, 252);
            alternativaIncorreta4.Size = new System.Drawing.Size(277, 20);
            alternativaIncorreta4.TabIndex = 10;

            alternativaCorreta = new TextBox();
            alternativaCorreta.Location = new System.Drawing.Point(484, 291);
            alternativaCorreta.Size = new System.Drawing.Size(277, 20);
            alternativaCorreta.TabIndex = 11;

            assunto = new TextBox();
            assunto.Location = new System.Drawing.Point(480, 66);
            assunto.Size = new System.Drawing.Size(277, 20);
            assunto.TabIndex = 13;

            assuntoDescricao = new TextBox();
            assuntoDescricao.Location = new System.Drawing.Point(480, 105);
            assuntoDescricao.Size = new System.Drawing.Size(277, 20);
            assuntoDescricao.TabIndex = 14;

            #endregion

            #region Button Config

            adicionarPergunta = new Button();
            adicionarPergunta.Location = new System.Drawing.Point(6, 317);
            adicionarPergunta.Size = new System.Drawing.Size(75, 23);
            adicionarPergunta.TabIndex = 0;
            adicionarPergunta.Text = "Adicionar";
            adicionarPergunta.UseVisualStyleBackColor = true;

            removerPergunta = new Button();
            removerPergunta.Location = new System.Drawing.Point(87, 317);
            removerPergunta.Size = new System.Drawing.Size(75, 23);
            removerPergunta.TabIndex = 1;
            removerPergunta.Text = "Remover";
            removerPergunta.UseVisualStyleBackColor = true;
            removerPergunta.Enabled = false;

            ativarEdicaoPergunta = new Button();
            ativarEdicaoPergunta.Location = new System.Drawing.Point(168, 317);
            ativarEdicaoPergunta.Size = new System.Drawing.Size(75, 23);
            ativarEdicaoPergunta.TabIndex = 2;
            ativarEdicaoPergunta.Text = "Editar";
            ativarEdicaoPergunta.UseVisualStyleBackColor = true;
            ativarEdicaoPergunta.Enabled = false;

            salvarPergunta = new Button();
            salvarPergunta.Location = new System.Drawing.Point(480, 317);
            salvarPergunta.Size = new System.Drawing.Size(111, 23);
            salvarPergunta.TabIndex = 12;
            salvarPergunta.Text = "Salvar Pergunta";
            salvarPergunta.UseVisualStyleBackColor = true;
            salvarPergunta.Enabled = false;

            adicionarAssunto = new Button();
            adicionarAssunto.Location = new System.Drawing.Point(6, 131);
            adicionarAssunto.Size = new System.Drawing.Size(75, 23);
            adicionarAssunto.TabIndex = 3;
            adicionarAssunto.Text = "Adicionar";
            adicionarAssunto.UseVisualStyleBackColor = true;

            removerAssunto = new Button();
            removerAssunto.Location = new System.Drawing.Point(87, 131);
            removerAssunto.Size = new System.Drawing.Size(75, 23);
            removerAssunto.TabIndex = 4;
            removerAssunto.Text = "Remover";
            removerAssunto.UseVisualStyleBackColor = true;
            removerAssunto.Enabled = false;

            ativarEdicaoAssunto = new Button();
            ativarEdicaoAssunto.Location = new System.Drawing.Point(168, 131);
            ativarEdicaoAssunto.Size = new System.Drawing.Size(75, 23);
            ativarEdicaoAssunto.TabIndex = 5;
            ativarEdicaoAssunto.Text = "Editar";
            ativarEdicaoAssunto.UseVisualStyleBackColor = true;
            ativarEdicaoAssunto.Enabled = false;

            salvarAssunto = new Button();
            salvarAssunto.Location = new System.Drawing.Point(480, 131);
            salvarAssunto.Size = new System.Drawing.Size(111, 23);
            salvarAssunto.TabIndex = 14;
            salvarAssunto.Text = "Salvar Assunto";
            salvarAssunto.UseVisualStyleBackColor = true;
            salvarAssunto.Enabled = false;

            voltar = new Button();
            voltar.Location = new System.Drawing.Point(705, 537);
            voltar.Size = new System.Drawing.Size(75, 23);
            voltar.TabIndex = 15;
            voltar.Text = "Voltar";
            voltar.UseVisualStyleBackColor = true;
            voltar.Click += new EventHandler(voltar_Click);

            #endregion

            #region GroupBox Config

            assuntoGBox = new GroupBox();
            assuntoGBox.SuspendLayout();

            perguntaGBox = new GroupBox();
            perguntaGBox.SuspendLayout();

            perguntaGBox.Controls.Add(perguntasView);
            perguntaGBox.Controls.Add(enunciadoLabel);
            perguntaGBox.Controls.Add(alternativaIncorreta1Label);
            perguntaGBox.Controls.Add(alternativaIncorreta2Label);
            perguntaGBox.Controls.Add(alternativaIncorreta3Label);
            perguntaGBox.Controls.Add(alternativaIncorreta4Label);
            perguntaGBox.Controls.Add(alternativaCorretaLabel);
            perguntaGBox.Controls.Add(enunciadoPergunta);
            perguntaGBox.Controls.Add(alternativaIncorreta1);
            perguntaGBox.Controls.Add(alternativaIncorreta2);
            perguntaGBox.Controls.Add(alternativaIncorreta3);
            perguntaGBox.Controls.Add(alternativaIncorreta4);
            perguntaGBox.Controls.Add(alternativaCorreta);
            perguntaGBox.Controls.Add(adicionarPergunta);
            perguntaGBox.Controls.Add(removerPergunta);
            perguntaGBox.Controls.Add(ativarEdicaoPergunta);
            perguntaGBox.Controls.Add(salvarPergunta);
            perguntaGBox.Location = new System.Drawing.Point(13, 12);
            perguntaGBox.Size = new System.Drawing.Size(767, 346);
            perguntaGBox.TabStop = false;
            perguntaGBox.Text = "Pergunta";

            assuntoGBox.Controls.Add(assuntosView);
            assuntoGBox.Controls.Add(assuntoLabel);
            assuntoGBox.Controls.Add(assunto);
            assuntoGBox.Controls.Add(assuntoDescricaoLabel);
            assuntoGBox.Controls.Add(assuntoDescricao);
            assuntoGBox.Controls.Add(adicionarAssunto);
            assuntoGBox.Controls.Add(removerAssunto);
            assuntoGBox.Controls.Add(ativarEdicaoAssunto);
            assuntoGBox.Controls.Add(salvarAssunto);
            assuntoGBox.Location = new System.Drawing.Point(13, 364);
            assuntoGBox.Size = new System.Drawing.Size(766, 167);
            assuntoGBox.TabStop = false;
            assuntoGBox.Text = "Assunto";

            #endregion

            this.Controls.Add(perguntaGBox);
            this.Controls.Add(assuntoGBox);
            this.Controls.Add(voltar);
            perguntaGBox.ResumeLayout(false);
            perguntaGBox.PerformLayout();
            assuntoGBox.ResumeLayout(false);
            assuntoGBox.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(perguntasView)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(assuntosView)).EndInit();
        }

        /*        internal void SetPerguntaGridView()        {            this.perguntasView.Columns[        }*/        void perguntasView_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            if (perguntasView.AreAllCellsSelected(false))
                ativarEdicaoPergunta.Enabled = removerPergunta.Enabled = false;
            else
                ativarEdicaoPergunta.Enabled = removerPergunta.Enabled = (e.StateChanged == DataGridViewElementStates.Selected);
        }

        #endregion

        void voltar_Click(object sender, EventArgs e)
        {
            ControladorGUI.Instance.ExibirMenuInicial();
        }

        #region GUI Components

        private GroupBox assuntoGBox;
        private GroupBox perguntaGBox;

        private DataGridView perguntasView;
        private DataGridViewTextBoxColumn perguntasViewEnunciado;
        private DataGridViewComboBoxColumn perguntasViewAssunto;
        private DataGridViewTextBoxColumn perguntasViewAlternativaCorreta;
        private DataGridViewTextBoxColumn[] perguntasViewAlternativasIncorretas;
        
        private DataGridView assuntosView;

        private Label enunciadoLabel;
        private Label alternativaIncorreta1Label;
        private Label alternativaIncorreta2Label;
        private Label alternativaIncorreta3Label;
        private Label alternativaIncorreta4Label;
        private Label alternativaCorretaLabel;

        private TextBox enunciadoPergunta;
        private TextBox alternativaIncorreta1;
        private TextBox alternativaIncorreta2;
        private TextBox alternativaIncorreta3;
        private TextBox alternativaIncorreta4;
        private TextBox alternativaCorreta;

        private Label assuntoLabel;
        private Label assuntoDescricaoLabel;

        private TextBox assunto;
        private TextBox assuntoDescricao;

        private Button adicionarPergunta;
        private Button removerPergunta;
        private Button ativarEdicaoPergunta;
        private Button salvarPergunta;

        private Button adicionarAssunto;
        private Button removerAssunto;
        private Button ativarEdicaoAssunto;
        private Button salvarAssunto;

        private Button voltar;

        #endregion
    }
}
